import {
  postUsersListData,
  postRolesListData,
  getAddRolesData,
  getDeleteRolesData,
  getEditRolesData
} from '@/service/main/system/system'
import { defineStore } from 'pinia'
import type { ISystemState } from './type'

const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    usersList: [],
    usersTotalCount: 0,
    entirRoles: []
  }),
  actions: {
    async postUsersListAction() {
      const usersListResult = await postUsersListData()
      const { totalCount, list } = usersListResult.data
      this.usersTotalCount = totalCount
      this.usersList = list
    },
    // 查角色列表
    async fetchEntirDataAction() {
      const rolesResult = await postRolesListData()

      this.entirRoles = rolesResult.data
    },
    // 新增角色
    async newRoleDataAction(data: any) {
      const roleAdd = await getAddRolesData(data)
      this.fetchEntirDataAction()
    },
    // 删除角色
    async deleteRoleDataAction(data: any) {
      const roleDelete = await getDeleteRolesData(data)
      this.fetchEntirDataAction()
    },
    // 编辑角色
    async editRoleDataAction(data: any) {
      const roleEdit = await getEditRolesData(data)
      console.log(roleEdit)
      this.fetchEntirDataAction()
    }
  }
})

export default useSystemStore
